package com.sino.servlet;

import javax.servlet.*;
import javax.servlet.http.*;

public class AdminFilter extends HttpServlet implements Filter {
	private FilterConfig filterConfig;

	// Handle the passed-in FilterConfig
	public void init(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
	}

	// Process the request/response pair
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) {
		try {
			boolean pass = false;
			String message = "";
			HttpServletRequest h_request = (HttpServletRequest) request;
			HttpServletResponse h_response = (HttpServletResponse) response;
			String url = h_request.getServletPath();
			if (!url.equals("/admin/login.jsp")
					&& !url.equals("/admin/checklogin.jsp")
					&& url.startsWith("/admin")
					&& !url.equals("/admin/zclogin.jsp")
					&& !url.equals("/admin/zcchecklogin.jsp")) {
				if (h_request.getSession().getAttribute("user") != null) {
					pass = true;
				} else if(h_request.getSession().getAttribute("logininfo")!=null){
					pass = true;
				}else{
					
					pass = false;
				
				}
				if (!pass) {
					String rootpath = h_request.getContextPath();
					String login_page = "/"
							+ rootpath.substring(1, rootpath.length())
							+ "/admin/login.jsp";
					RequestDispatcher rd = request
							.getRequestDispatcher(login_page);
					h_response.sendRedirect(login_page);
				} else {
					filterChain.doFilter(request, response);
				}
			} else {
				filterChain.doFilter(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Clean up resources
	public void destroy() {
	}
}
